package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Advertising;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.AdvertisingDao;
import com.weipai.springboot.module.service.AdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2019/01/09 09:47
 */
@Service
public class AdvertisingServiceImpl implements AdvertisingService {
    
    @Autowired
    private AdvertisingDao advertisingDao;

    @Override
    public Response list(Integer page, Integer size, String draw, String type) {
        Integer start = (page-1) * size;
        List<Advertising> inventories = advertisingDao.list(start, size, type);
        Integer totalNum = advertisingDao.amount(type);
        PageResult<Advertising> pageList = new PageResult<>(inventories,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response save(Advertising advertising) {
        Response response = new Response();
        if(advertising.getId()!=null){
            advertisingDao.updateByPrimaryKeySelective(advertising);
        }else{
            advertising.setStatus(0);
            advertisingDao.insert(advertising);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response changeStatus(String id) {
        Response response = new Response();
        Advertising advertising = advertisingDao.selectByPrimaryKey(id);
        if(advertising!=null){
            if(advertising.getStatus()==0){
                advertising.setStatus(-1);
            }else{
                advertising.setStatus(0);
            }
            advertisingDao.updateByPrimaryKeySelective(advertising);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response delete(String id) {
        Response response = new Response();
        advertisingDao.deleteByPrimaryKey(id);
        response.setCode(RetCode.SUCCESS);
        return response;
    }
}
